<?php
namespace frontend\controllers;

use Yii;
use common\lib\Page;
use common\models\Tags;
use frontend\models\Novels;

class TypeController extends PublicController
{
    public function actionIndex()
    {
        $id = intval(Yii::$app->request->get('id'));
        $page = intval(Yii::$app->request->get('page'));
        $type = Tags::find()->where(['tagId' => $id])->asArray()->one();
        $where = ['typeId' => $id];
        $m = Novels::find();
        $p = new Page();
        $p->setWhere($where);
        $p->setModel($m);
        $p->setPageSize(30);
        $p->setCurrentPage(isset($page) ? $page : 1);
        $list = $p->getPage();
        foreach((array)$list['data'] as $k => $v){
            $face = $v['face'];
            $list['data'][$k]['face'] = $this->getFace($face);
        }
        $data = [
            'id' => $id,
            'type' => $type,
            'data' => $list['data'],
            'pageStr' => $list['pageStr'],
        ];
        return $this->render('index', $data);
    }
    
    private function getFace($pic){
        if(empty($pic)){
            return Yii::$app->params['pic'].'nocover.jpg';
        }else{
            $fileName = basename($pic);
            $realPath = '/opt/lampp/htdocs/testswoole/Pic/'.$fileName;
            if(!file_exists($realPath)){
                return Yii::$app->params['pic'].'nocover.jpg';
            }else{
                return Yii::$app->params['pic'].$fileName;
            }
        }
    }
}
